package com.itheima.test12;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author:gy
 * @Date: 2025/09/01 15:22
 * @Description:
 */
public class MyThread extends Thread{
    //定义一个成员变量: 100张票
    private static int ticketCount = 100;
    private static ReentrantLock lock = new ReentrantLock();
    @Override
    public void run() {
        while (true) {
            try {
                lock.lock();
                if (ticketCount <= 0) {
                    //不能卖了
                    break;
                } else {
                    //可以卖票
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    ticketCount--;
                    System.out.println(Thread.currentThread().getName() + "正在卖票,还剩下" + ticketCount + "张票");
                }
            } catch (RuntimeException e) {
                throw new RuntimeException(e);
            } finally {
                lock.unlock();
            }

        }

       }
    }


